﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Modularity;


namespace OpenPasteSpider
{
    /// <summary>
    /// 
    /// </summary>
    [DependsOn(
        typeof(OpenPasteSpiderDomainModule),
        typeof(AbpEntityFrameworkCoreModule)
    )]
    public class OpenPasteSpiderEntityFrameworkCoreModule : AbpModule
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            //Console.WriteLine("--- OpenPasteSpiderEntityFrameworkCoreModule.ConfigureServices ---");

            //var Configuration = context.Services.GetConfiguration();
            //var configlist = Configuration.AsEnumerable();
            //var result = configlist.Where(x => x.Key.StartsWith("SpiderConfig:")).ToDictionary(x => x.Key.Replace("SpiderConfig:", ""), x => x.Value);
            //var config = Newtonsoft.Json.JsonConvert.DeserializeObject<SpiderConfig>(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            //Console.WriteLine(context.Services.GetConfiguration().GetConnectionString("MainConnectionString"));

            ////pgsql
            //context.Services.AddTransient<IOpenPasteSpiderDbContext,OpenPasteSpiderDbContext>();
            //context.Services.AddAbpDbContext<OpenPasteSpiderDbContext>(op =>
            //{
            //    Configure<AbpDbContextOptions>(aa => { aa.UseNpgsql(); });
            //});
            //AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
            //context.Services.AddAbpDbContext<OpenPasteSpiderDbContext>(options =>
            //{
            //    options.AddDefaultRepositories<OpenPasteSpiderDbContext>(includeAllEntities: true);
            //});

            context.Services.AddTransient<IOpenPasteSpiderDbContext, SqliteDbContext>();
            context.Services.AddAbpDbContext<SqliteDbContext>(op =>
            {
                Configure<AbpDbContextOptions>(aa => { aa.UseSqlite(); });
            });
            //AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
            context.Services.AddAbpDbContext<SqliteDbContext>(options =>
            {
                options.AddDefaultRepositories<SqliteDbContext>(includeAllEntities: true);
            });

        }

        /// <summary>
        /// 
        /// </summary>
        private static IConfigurationRoot _config { get; set; } = null;

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static IConfigurationRoot BuildConfiguration(string[] args)
        {
            if (_config != null)
            {
                return _config;
            }
            var builder = new ConfigurationBuilder()
                .SetBasePath(AppContext.BaseDirectory)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddEnvironmentVariables();
            //.AddCommandLine(args)
            //.Build();
            if (args != null)
            {
                builder.AddCommandLine(args);
            }

            _config = builder.Build();

            return _config;
        }

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <returns></returns>
        //public static IConfigurationRoot BuildConfiguration()
        //{
        //    var builder = new ConfigurationBuilder()
        //        .SetBasePath(Directory.GetCurrentDirectory())
        //        .AddJsonFile("appsettings.json", optional: false)
        //        .AddJsonFile($"appsettings.Development.json", optional: true)
        //        .AddEnvironmentVariables();

        //    return builder.Build();
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public override void PreConfigureServices(ServiceConfigurationContext context)
        {
            //Console.WriteLine("--- OpenPasteSpiderEntityFrameworkCoreModule.PreConfigureServices ---");

            base.PreConfigureServices(context);
        }
    }
}